//
//  KLBookListTableViewCell+BookEntity.m
//  KLBookCollection
//
//  Created by apple on 2017/10/17.
//  Copyright © 2017年 bonc. All rights reserved.
//

#import "KLBookListTableViewCell+BookEntity.h"

#import "KLBookEntity.h"
#import "KLBookAuthor.h"
#import "KLBookTag.h"

#import "UIImageView+WebCache.h"
#import "NSArray+Extension.h"

@implementation KLBookListTableViewCell (BookEntity)

- (void)configureWithBookEntity:(KLBookEntity *)bookEntity {
    //title
    self.titleLabel.text = bookEntity.title;
    //author
    if ([bookEntity.authors isNotEmptyArray]) {
        NSString *authorList = @"";
        for (KLBookAuthor *author in bookEntity.authors) {
            authorList = [[authorList stringByAppendingString:author.name] stringByAppendingString:@" "];
        }
        self.authorLabel.text = [NSString stringWithFormat:@"作者：%@",authorList];
    } else {
        self.authorLabel.text = @" ";
    }
    //image
//    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:bookEntity.image]];
    //tags
    UIView *lastDockView = self.tagsView;
    for (NSUInteger i = 0; i<MIN(bookEntity.tags.count, 5); i++) {
        KLBookTag *tag = [bookEntity.tags objectAtIndex:i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:UIColorFromHexRGB(0x999999) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:9.0f];
        button.layer.cornerRadius = 2.0f;
        button.layer.borderColor = UIColorFromHexRGB(0x999999).CGColor;
        button.layer.borderWidth = 0.5f;
        button.translatesAutoresizingMaskIntoConstraints = NO;
        [button setContentEdgeInsets:UIEdgeInsetsMake(3.0f, 5.0f, 3.0f, 5.0f)];
        [button setTitle:tag.name forState:UIControlStateNormal];
        [button sizeToFit];
        
        [self.tagsView addSubview:button];
        
        if ([lastDockView isEqual:self.tagsView]) {
            [self.tagsView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[button]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(button)]];
            [self.tagsView addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.tagsView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
        } else {
            [self.tagsView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lastDockView]-8-[button]" options:NSLayoutFormatAlignAllCenterY metrics:nil views:NSDictionaryOfVariableBindings(lastDockView,button)]];
        }
        lastDockView = button;
    }
    
    [self.tagsView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[lastDockView]-(>=0)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastDockView)]];
}

- (void)startDownloadCoverImageWithBookEntity:(KLBookEntity *)bookEntity {
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:bookEntity.image]];
}

@end
